package com.wuze.cloud.adapter;

import com.wuze.cloud.dao.SysFolderDao;
import com.wuze.cloud.exception.SysGlobalException;
import com.wuze.cloud.pojo.SysFileDO;
import com.wuze.cloud.pojo.SysFolderDO;
import com.wuze.cloud.pojo.SysFolderRelationDO;
import com.wuze.cloud.pojo.SysUserDO;
import com.wuze.cloud.request.SysCreateFolderQuery;
import com.wuze.cloud.response.SysFolderListResponse;
import com.wuze.cloud.response.SysFolderVO;
import com.wuze.cloud.response.SysShareFolderResponse;
import com.wuze.cloud.util.SnowFlakeIdWorker;
import org.assertj.core.util.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static com.wuze.cloud.enums.SysFileTypeEnum.FOLDER_FILE;
import static com.wuze.cloud.enums.SysGlobalExceptionEnum.SHARE_FOLDER_EMPTY;
import static com.wuze.cloud.enums.SysStateEnum.CREATE;

/**
 * @author: wuze
 * @description:
 * @date: 2023/02/18
 */
@Component
public class SysFolderAdapter {

    @Autowired
    private SysFolderDao folderDao;


    public SysFolderDO buildSysFolderDO(SysCreateFolderQuery query , SysUserDO user){
        SysFolderDO folder = new SysFolderDO();

        folder.setId(SnowFlakeIdWorker.nextId());
        folder.setFolderName(query.getFolderName());
        folder.setPid(query.getPid());
        folder.setState(CREATE.name());
        folder.setUserId(user.getId());

        return folder;
    }

    public SysFolderVO buildSysFolderVO(SysFolderDO folder){
        SysFolderVO vo = new SysFolderVO();
        if(Objects.isNull(folder)){
            return null;
        }
        BeanUtils.copyProperties(folder,vo);
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
        vo.setCreateTime(format.format(folder.getCreateTime()));
        vo.setUpdateTime(format.format(folder.getUpdateTime()));
        return vo;
    }

    public List<SysFolderVO> buildSysFolderVOS(List<SysFolderDO> folders){
        List<SysFolderVO> vos = new ArrayList<>();
        folders.stream().forEach(folder -> {
            vos.add(buildSysFolderVO(folder));
        });
        return vos;
    }

    public SysFolderRelationDO buildRelationDO(SysUserDO user , SysFolderDO folder){
        SysFolderRelationDO relationDO = new SysFolderRelationDO();
        relationDO.setId(SnowFlakeIdWorker.nextId());
        relationDO.setUserId(user.getId());
        relationDO.setPid(folder.getPid());
        relationDO.setChildId(folder.getId());
        relationDO.setChildName(folder.getFolderName());
        relationDO.setState(CREATE.name());
        relationDO.setType(FOLDER_FILE.name());
        return relationDO;
    }


    public SysFolderRelationDO buildRelationDO(SysFileDO file){
        SysFolderRelationDO relationDO = new SysFolderRelationDO();
        relationDO.setId(SnowFlakeIdWorker.nextId());
        relationDO.setUserId(file.getUserId());
        relationDO.setPid(file.getFolderId());
        relationDO.setChildId(file.getId());
        relationDO.setChildName(file.getFileName());
        relationDO.setState(CREATE.name());
        relationDO.setType(file.getType());
        return relationDO;
    }

    public SysFolderListResponse buildSysFolderListResponse(Long folderId , String state){
        SysFolderDO folderDO = folderDao.findById(folderId);
        SysFolderVO vo = buildSysFolderVO(folderDO);

        List<SysFolderDO> folderDOS = folderDao.findByPid(folderId, state);
        List<SysFolderListResponse> list = Lists.newArrayList();
        folderDOS.forEach(folder->{
            list.add(buildSysFolderListResponse(folder.getId(),state));
        });

        SysFolderListResponse res = new SysFolderListResponse();
        res.setFolder(vo);

        res.setChildFolders(list);
        return res;
    }

    public SysShareFolderResponse buildSysShareFolderResponse(Long folderId , String state){
        SysFolderDO folderDO = folderDao.findById(folderId);
        if(!folderDO.getState().equals(CREATE.name())){
            throw new SysGlobalException(SHARE_FOLDER_EMPTY.getCode(), SHARE_FOLDER_EMPTY.getMsg());
        }
        SysFolderVO vo = buildSysFolderVO(folderDO);

        List<SysFolderDO> folderDOS = folderDao.findByPid(folderId, state);
        List<SysShareFolderResponse> list = Lists.newArrayList();
        folderDOS.forEach(folder->{
            list.add(buildSysShareFolderResponse(folder.getId(),state));
        });

        SysShareFolderResponse res = new SysShareFolderResponse();
        res.setFolder(vo);

        res.setChildFolders(list);
        return res;
    }
}
